package com.sc.nft.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.helper.LlPayHelper;
import com.sc.nft.helper.SandAccountHelper;
import com.sc.nft.service.BlockChainTypeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RequiredArgsConstructor
@Slf4j
public class SandTask {


    private final SandTransferRecordDao sandTransferRecordDao;
    private final SandWithdrawRecordDao sandWithdrawRecordDao;
    private final DaoWithdrawRecordDao daoWithdrawRecordDao;
    private final SandAccountHelper sandAccountHelper;
    private final LlPayHelper llPayHelper;

    /**
     * 检查转账中的记录
     * @throws Exception
     */
    @XxlJob("checkSandTransfer")
    public void checkSandTransfer() throws Exception {
        List<SandTransferRecord> sandTransferRecords = sandTransferRecordDao.transferList();
        for (SandTransferRecord sandTransferRecord : sandTransferRecords) {
            sandAccountHelper.checkTransfer(sandTransferRecord);
        }
    }
    /**
     * 检查体现中的记录
     * @throws Exception
     */
    @XxlJob("checkSandWithdraw")
    public void checkSandWithdraw() throws Exception {
        List<SandWithdrawRecord> sandWithdrawRecords = sandWithdrawRecordDao.syncStatus();
        for (SandWithdrawRecord sandWithdrawRecord : sandWithdrawRecords) {
            switch (sandWithdrawRecord.getChannel()) {

                case SAND:
                    sandAccountHelper.checkWithdraw(sandWithdrawRecord);
                    break;
                case LL:
                    try {
                        JSONObject withdrawResult = llPayHelper.findWithdrawResult(sandWithdrawRecord.getCustomerOrderNo());
                        if(StrUtil.equals(withdrawResult.getString("txn_status"),"TRADE_SUCCESS")){
                            sandWithdrawRecord.setStatus(1);
                            sandWithdrawRecord.updateById();
                        }else if(StrUtil.equals(withdrawResult.getString("txn_status"),"TRADE_PREPAID")){
                            sandWithdrawRecord.updateById();
                        }else if(StrUtil.equals(withdrawResult.getString("txn_status"),"TRADE_FAILURE")){
                            sandWithdrawRecord.setStatus(2);
                            sandWithdrawRecord.updateById();
                        }else{
                            sandWithdrawRecord.setRemark(withdrawResult.getString("failure_reason"));
                            sandWithdrawRecord.setStatus(2);
                            sandWithdrawRecord.updateById();
                        }
                    }catch (GlobalRunTimeException e){
                        sandWithdrawRecord.setStatus(2);
                        sandWithdrawRecord.setRemark(StrUtil.EMPTY);
                        sandWithdrawRecord.updateById();
                    }
                    break;
            }
        }
    }



    /**
     * DAO检查提现中的记录
     * @throws Exception
     */
    @XxlJob("daoWithdraw")
    public void daoWithdraw() throws Exception {
        List<DaoWithdrawRecord> withdrawRecords = daoWithdrawRecordDao.syncStatus();
        for (DaoWithdrawRecord daoWithdrawRecord : withdrawRecords) {
            switch (daoWithdrawRecord.getChannel()) {
                case LL:
                    try {
                        JSONObject withdrawResult = llPayHelper.findWithdrawResult(daoWithdrawRecord.getCustomerOrderNo());
                        if(StrUtil.equals(withdrawResult.getString("txn_status"),"TRADE_SUCCESS")){
                            daoWithdrawRecord.setStatus(1);
                            daoWithdrawRecord.updateById();
                        }else if(StrUtil.equals(withdrawResult.getString("txn_status"),"TRADE_PREPAID")){
                            daoWithdrawRecord.updateById();
                        }else{
                            daoWithdrawRecord.setRemark(withdrawResult.getString("failure_reason"));
                            daoWithdrawRecord.setStatus(2);
                            daoWithdrawRecord.setRemark(StrUtil.EMPTY);
                            daoWithdrawRecord.updateById();
                        }
                    }catch (GlobalRunTimeException e){
                        daoWithdrawRecord.setStatus(2);
                        daoWithdrawRecord.setRemark(StrUtil.EMPTY);
                        daoWithdrawRecord.updateById();
                    }
                    break;
            }
        }
    }


}
